package com.xjj.service.health.mapper;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.xjj.entity.Health;
import com.xjj.entity.Leave;
import com.xjj.vo.HealthVo;
import com.xjj.vo.LeaveVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.jdbc.SQL;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @Auther: AYang
 * @Date: 2022/10/28 09:26
 * @Description:
 */
@Repository
public interface HealthMapper extends BaseMapper<Health> {
    @SelectProvider(type = HealthSqlProvider.class, method = "select")
    @Results(id = "healthMap", value = {
            @Result(id = true, column = "id", property = "id"),
            @Result(column = "parent_id", property = "parentId"),
            @Result(column = "text", property = "text"),
            @Result(column = "date", property = "date"),
            @Result(column = "status", property = "status"),
            @Result(column = "send_time", property = "sendTime"),
            @Result(column = "check_time", property = "checkTime"),
            @Result(column = "img", property = "img"),
            @Result(column = "manager_id", property = "managerId")
    })
    List<Health> getByCondition(Health health);

    @Update("update xjj_health set status = #{status},checkTime = #{checkTime} where id = #{id}")
    void updateStatusById(@Param("id") Integer id, @Param("status") String status,@Param("checkTime") String checkTime);

    @Insert("insert into xjj_health(id,img) values(#{healthId},#{img})")
    void addHealthImg(@Param("id") Integer id, String img);


    @Slf4j
    class HealthSqlProvider{
        /**
         * 多条件查询
         * @param health
         * @return
         */
        public String select(Health health){
            return new SQL(){
                {
                    SELECT("h.id,h.text,h.date,h.date,h.send_time,h.img,h.status,s.name AS studentName,p.name AS parentName,k.name AS klassName").FROM("xjj_health h")
                            .LEFT_OUTER_JOIN("xjj_student s ON s.id = h.student_id",
                                    "xjj_parent p ON s.parent_id = p.id",
                                    "xjj_klass k ON s.klass_id = k.id");
                    if (health.getStudentId() != null){
                        WHERE("student_id = #{studentId}");
                    }
                    if (StringUtils.hasLength(health.getText())){
                        WHERE("text=#{text}");
                    }
                    if (StringUtils.hasLength(health.getDate())){
                        WHERE("date=#{date}");
                    }
                    if (StringUtils.hasLength(health.getStatus())){
                        WHERE("status=#{status}");
                    }
                    if (StringUtils.hasLength(health.getSendTime())){
                        WHERE("send_time=#{sendTime}");
                    }
                    if (StringUtils.hasLength(health.getCheckTime())){
                        WHERE("check_time=#{checkTime}");
                    }
                    if (StringUtils.hasLength(health.getImg())){
                        WHERE("img=#{img}");
                    }
                    if (StringUtils.hasLength(health.getStudentName())){
                        WHERE("s.name=#{studentName}");
                    }
                    if (StringUtils.hasLength(health.getKlassName())){
                        WHERE("k.id=#{klassName}");
                    }
                    ORDER_BY("send_time DESC");
                }
            }.toString();
        }
    }
}
